package indi.zhifa.engine.test.service.impl;

import indi.zhifa.engine.core.common.entity.web.ServiceException;
import indi.zhifa.engine.mpenhance.utils.DbDtoEntityUtil;
import indi.zhifa.engine.core.common.ann.caches.CacheGet;
import indi.zhifa.engine.core.common.ann.caches.CacheParam;
import indi.zhifa.engine.test.dao.dbservice.IGenUserDbService;
import indi.zhifa.engine.test.entity.dto.CreateUserDto;
import indi.zhifa.engine.test.entity.po.GenUserEntity;
import indi.zhifa.engine.test.enums.EUserStatus;
import indi.zhifa.engine.test.service.IAuthService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@RequiredArgsConstructor
@Component
public class AuthServiceImpl implements IAuthService {

    final IGenUserDbService mGenUserDbService;

    @Override
    public GenUserEntity createUser(CreateUserDto pUserDto) {

        if(mGenUserDbService.existByUserName(pUserDto.getUserName())) {
            throw new ServiceException("已经存在用户名为"+pUserDto.getUserName()+"的用户");
        }
        GenUserEntity genUserEntity = DbDtoEntityUtil.createFromDto(pUserDto, GenUserEntity.class);
        genUserEntity.setStatus(EUserStatus.NORMAL);
        genUserEntity.setHasDep(false);
        genUserEntity.setHasPosition(false);
        genUserEntity.setCurVersion("1.0");
        mGenUserDbService.save(genUserEntity);
        return genUserEntity;
    }

    @CacheGet(prefix = "userId",expression = "#id")
    @Override
    public GenUserEntity info(@CacheParam(name = "id") Long pId) {
        return mGenUserDbService.check(pId);
    }
}
